<template>
<view>
<!--pages/productDetail/index.wxml-->

<view class="organize_detail_container" v-if="organizeData">

<view class="organize_detail_wiper_container">
    <swiper class="organize_detail_wiper_index" autoplay="true" interval="5000" duration="500" @change="swiperChange">
      <block v-for="(item, swiper) in imgArr" :key="swiper" style="border-radius: 20rpx">
        <swiper-item :data-imageUrl="imgArr" :data-index="index" @tap="lookBigImage" style="border-radius: 20rpx">
          <image mode="aspectFit" :src="item.firstImage + '?x-oss-process=style/normal'" style="border-radius: 20rpx"></image>
        </swiper-item> 
      </block>
    </swiper>
    <view class="swiperItemIndex">{{swiperIndex}}/{{totalImg}}</view>
    <block v-if="organizeData.promotionBean">
      <view class="timeCountDownBg" :style="'background:linear-gradient(116deg,' + setting.platformSetting.secondColor + ' 0%,' + color + ' 100%);'" v-if="organizeData.promotionBean.promotionStatus==1"></view>
      <view class="timeCountDownBg" style="background:linear-gradient(116deg,#eee 0%,#999 100%);" v-else></view>
      <view class="timeCountDown" v-if="organizeData.promotionBean.promotionStatus!=0">
        <image class="timeLeftImg" src="http://image.tunzai.vip/tunzai/2018_8/29/10/29/14_880.jpg" v-if="organizeData.promotionBean.promotionStatus==1"></image>
        <image class="timeLeftImg" src="http://image.tunzai.vip/tunzai/2018_9/12/17/6/41_92.jpg" v-else></image>
        <view class="timeItem"><text class="timeText">距结束仅剩</text><time v-if="organizeData!=null" :receiveData="organizeData.promotionBean.endDate"></time></view>
      </view>
    </block>
  </view>  
  <!-- 空间内容 -->
  <view class="space_detail_head">
    <image class="space_detail_logo" :src="organizeData.icon"></image>
    <view class="space_detail_center">
      <text class="space_detail_name">{{organizeData.name}}</text>
      <view class="space_detail_item">
        <image class="space_detail_icon" src="http://image1.sansancloud.com/xianhua/2019_1/23/16/8/24_251.jpg"></image>
        <text class="space_detail_value">{{organizeData.contactName}}</text>
      </view>
      <view class="space_detail_item">
        <image class="space_detail_icon" src="http://image1.sansancloud.com/xianhua/2019_1/23/16/8/24_279.jpg"></image>
        <text class="space_detail_value">{{organizeData.contactTelno}}</text>
      </view>
    </view>
  </view>
  <!-- 空间简介 -->
  <view class="space_description" style="margin-top:20rpx;">
    <text class="space_description_text">{{organizeData.description}}</text>
  </view>

    <!-- 首页组件-私有产品详情装饰-->
    <view>
     <custom-page-index-component :receiveData="sendOrganizeData" v-if='sendOrganizeData'></custom-page-index-component>
    </view>
    <!-- 首页组件-公共产品详情装饰-->
    <custom-page-index-component :receiveData="sendIndexData" v-if='sendIndexData'></custom-page-index-component>
    <!-- 富文本 -->
    <view class="product_main_info" v-if="assetData.text!=''&&assetData.text!='<p><br></p>'">
      <view class="line"></view>
      <text class="rich_title">简介</text>
    </view>
    <view class="tab_detail_container" v-if="assetData.text!=''&&assetData.text!='<p><br></p>'">
       <view class="wxParseHtmlContainer">
            <rich-text-component :receiveData="articleJsonData" v-if="articleJsonData"></rich-text-component>
            <text class="no_rich" v-if="!articleJsonData">暂无介绍~</text>
		   <!-- <component is="init" :wxParseData="article.nodes"></component> -->
		</view>
    </view>
</view>



<posters-component :proId="proId" :shopId="shopId" :ewmImgUrl="qrCodeUrl" v-if="posterState" @closePoaster="getChilrenPoster"></posters-component>
</view>
</template>

<script>
import publicFun from '../../wxcomponents/public/publicFun.wxs'
export default {
  data() {
    return {
      setting: null,
      // setting 
      organizeData: null,
      // 商品数据 
      targs: null,
      posterState: false,
      qrCodeUrl: "",
      swiperIndex: 1,
      totalImg: 0,
      color: '',
      secondColor: "",
      sendOrganizeData: "",
      sendIndexData: "",
      dataFOr_getData: {
        organizeId: ''
      },
      pintuanState: false,
      imgArr: [],
      organizeId: "",
      articleJsonData:'',
    };
  },

  props: {},
  onError: function (options) {
    console.log("on error!!!");
  },
  onLoad: function (options) {
    this.publicFun = publicFun;
    console.log('--------product----------', options);
    let that = this;
    that.setData({
      setting: getApp().globalData.setting
    });
    that.setData({
      organizeId: options.organizeId,
      color: getApp().globalData.setting.platformSetting.defaultColor,
      secondColor: getApp().globalData.setting.platformSetting.secondColor
    });
    let sendIndexData = JSON.stringify({
      title: 'noTitle',
      url: "organize",
      params: {
        pageObjectId: options.organizeId
      }
    });
    that.setData({
      sendIndexData: sendIndexData
    });
    let sendOrganizeData = JSON.stringify({
      title: 'noTitle',
      url: "organize_" + options.organizeId
    });
    that.setData({
      sendOrganizeData: sendOrganizeData
    });
    console.log("商品id和店铺id", options);
    that.dataFOr_getData.organizeId = options.organizeId;
    that.setData({
      dataFOr_getData: that.dataFOr_getData
    });
    that.getData(options);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
        let that=this;
        that.setData({
          setting: getApp().globalData.setting
        });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareTimeline:function(){
    return this.shareFun()
  },
  onShareAppMessage: function (res) {
    console.log(res);
    return this.shareFun()
  },
  methods: {
    shareFun:function(){
        let that = this;
        let params = that.dataFOr_getData;
        let productItem = that.organizeData;
        
        if (!productItem.brandName || productItem.brandName == "") {
          productItem.brandName = "";
        }
        
        ;
        let shareName = '活动价：￥' + productItem.price + '(原价：￥' + productItem.tagPrice + ')' + productItem.brandName + productItem.name;
        console.log('params:', params, that.organizeData);
        return getApp().globalData.shareForFx2('productDetail', shareName, params);
    },
    /*轮播图下标*/
    swiperChange: function (e) {
      this.setData({
        swiperIndex: e.detail.current + 1
      });
    },

    // 关闭海报
    getChilrenPoster(e) {
      let that = this;
      that.setData({
        posterState: false
      });
    },

    showPoster: function () {
      console.log('===showPoster====', getApp().globalData.clientNo);
      let that = this;
      this.getQrCode();
      that.setData({
        posterState: true
      });
    },
    toIndex: function () {
      getApp().globalData.toIndex();
    },
    posterStateFun: function (state) {
      console.log('====state====', state);
      this.setData({
        posterState: true
      });
    },
    tolinkUrl: function (e) {
      if (!getApp().globalData.loginUser) {
        wx.showModal({
          title: '提示',
          content: '主人~您还在登陆哦!稍等片刻',
          success: function (res) {
            if (res.confirm) {
              console.log('用户点击确定');
            } else if (res.cancel) {
              console.log('用户点击取消');
            }
          }
        });
        return;
      }

      let linkUrl = e.currentTarget.dataset.link;
      getApp().globalData.linkEvent(linkUrl);
    },
    lookBigImage: function (e) {
      console.log("111111111", e.currentTarget.dataset);
      let imgSrc = e.currentTarget.dataset.imageurl;
      let imgArray = [];
      let index = e.currentTarget.dataset.index;
      let PostImageSrc = [];
      console.log(imgSrc);

      for (let i = 0; i < imgSrc.length; i++) {
        imgArray.push(imgSrc[i].imagePath);
        PostImageSrc.push(imgSrc[i].imagePath.replace(/http/, "https"));
      } // let PostImageSrc = imgSrc


      console.log(PostImageSrc);

      if (!imgSrc) {
        return;
      } // let urls = []
      // urls.push(imgSrc)


      wx.previewImage({
        current: imgArray[index],
        // 当前显示图片的http链接
        urls: imgArray // 需要预览的图片http链接列表

      });
    },

    /**
     * 生命周期函数--监听页面加载
     */
    getData: function (options) {
      let param = {};
      let that = this;

      if (!options) {
        param = that.dataFOr_getData;
      } else {
        param = options;
      } // wx.showLoading({
      //   title: 'loading'
      // })


      getApp().globalData.showToastLoading('loading', true);
      console.log('==param===', param);
      let postParam = {};
      postParam.organizeId = param.organizeId;
      let customIndex = getApp().globalData.AddClientUrl("/wx_get_asset_organize_detail.html", postParam);
      wx.request({
        url: customIndex.url,
        header: getApp().globalData.header,
        success: function (res) {
          console.log(res);
          that.setData({
            pintuanState: false
          });
          console.log('--------------getData-------------');
          that.setData({
            organizeData: res.data.relateObj
          });

          if (res.data.relateObj.firstImage) {
            that.setData({
              totalImg: 1,
              imgArr: [{
                firstImage: res.data.relateObj.firstImage
              }]
            });
          }

          if (res.data.relateObj.text) {
            //WxParse.wxParse('article', 'html', res.data.relateObj.text, that, 10)
            // setTimeout(function () {
            //   that.article_article = res.data.relateObj.text;
            // });
            // console.log('====article====', that.article);
            let articleJsonData=res.data.relateObj.text
            that.setData({
              articleJsonData:{jsonData:{content:articleJsonData}}
            });
            console.log('====articleJsonData====', that.articleJsonData);
          }
          console.log('organizeData', that.organizeData);
        },
        fail: function (res) {
          console.log("====fail=====");
          getApp().globalData.loadFail();
        },
        complete: function (res) {
          wx.hideLoading();
        }
      });
    },
    // 获取二维码
    getQrCode: function () {
      let userId = "";

      if (getApp().globalData.loginUser && getApp().globalData.loginUser.platformUser) {
        userId = 'MINI_PLATFORM_USER_ID_' + getApp().globalData.loginUser.platformUser.id;
      } // console.log("app.loginUser.platformUser", app.loginUser.platformUser.id)
      // path=pageTab%2findex%2findex%3fAPPLY_SERVER_CHANNEL_CODE%3d'


      let postParam = {};
      postParam.SHARE_PRODUCT_DETAIL_PAGE = this.proId;
      postParam.scene = userId; // 上面是需要的参数下面的url

      console.log('====pp======', "/super_shop_manager_get_mini_code.html?mini=1&path=pageTab%2findex%2findex%3fSHARE_PRODUCT_DETAIL_PAGE%3d" + this.proId + "%26scene%3d" + userId);
      var customIndex = getApp().globalData.AddClientUrl("/super_shop_manager_get_mini_code.html?mini=1&path=pageTab%2findex%2findex%3fSHARE_PRODUCT_DETAIL_PAGE%3d" + this.proId + "%26scene%3d" + userId, postParam, 'get', '1');
      var result = customIndex.url.split("?");
      customIndex.url = result[0] + "?" + result[1];
      console.log("customIndex", customIndex.url, result[0]);
      var that = this;
      that.setData({
        qrCodeUrl: customIndex.url
      });
    },
    // 定位
    clickCatch: function (e) {
      console.log(this.organizeData);
      let latitude = this.organizeData.latitude;
      let longitude = this.organizeData.longitude;
      let name = this.organizeData.name;
      let address = this.organizeData.province + this.organizeData.city + this.organizeData.area + this.organizeData.address;
      wx.openLocation({
        latitude: latitude,
        longitude: longitude,
        scale: 12,
        name: name,
        address: address
      });
    },
    setData: function (obj) {
      let that = this;
      let keys = [];
      let val, data;
      Object.keys(obj).forEach(function (key) {
        keys = key.split('.');
        val = obj[key];
        data = that.$data;
        keys.forEach(function (key2, index) {
          if (index + 1 == keys.length) {
            that.$set(data, key2, val);
          } else {
            if (!data[key2]) {
              that.$set(data, key2, {});
            }
          }

          data = data[key2];
        });
      });
    }
  }
};
</script>
<style>
@import "./index.css";
</style>